<?php

namespace App\Console\Commands\OpenShop\Ekb;

use App\Models\Ekb\EkbApplyHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserContractModel;
use App\Remote\EKuaiBao\EKuaiBaoRemote;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class RefreshEkbApplyHistoryRefundAmountCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ekb:RefreshEkbApplyHistoryRefundAmount';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '易快报：刷新退款金额';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        EkbApplyHistoryModel::query()->where("refundAmount", 0)->where("status", 0)
                            ->chunkById(100, function (Collection $collection) {
                                $collection->each(function (EkbApplyHistoryModel $ekbApplyHistoryModel) {
                                    $content                            = json_decode(
                                        $ekbApplyHistoryModel->content,
                                        true
                                    );
                                    $ekbApplyHistoryModel->refundAmount = $content["form"]["details"][0]["feeTypeForm"]["amount"]["standard"] * 100;
                                    $ekbApplyHistoryModel->save();
                                });
                            });
        return 0;
    }
}
